package com.cuitrip.component.item;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Adapter;
import android.widget.ListView;

import com.cuitrip.business.user.Indexer;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;

import java.util.ArrayList;

/**
 * Created by sheena on 15/12/8.
 */
public class IndexBar extends View {
    private Paint mPaint;
    /*private static final String[] LETTERS = new String[] { "A", "B", "C", "D", "E", "F", "G", "H",
            "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y",
            "Z" };*/
    private int mHeight;
    private int mCellWidth;
    private float mCellHeight;
    private Rect mRect;
    private int mIndex = -1;
    private ArrayList<String> letters;
    private ListView mListView;
    private int wordSize = 16;
    private int preHeight = wordSize + 2;
    private int heightCenter;
    private Indexer mSectionIndexter;

    public void test(){
        postInvalidate();
    }
    public void setLetters(ArrayList<String> letters) {
        this.letters = letters;
    }

    public interface OnLetterChangeListener {
        void onLetterChange(String letter);
    }

    private OnLetterChangeListener mOnLetterChangeListener;

    public OnLetterChangeListener getOnLetterChangeListener() {
        return mOnLetterChangeListener;
    }

    public void setOnLetterChangeListener(OnLetterChangeListener onLetterChangeListener) {
        mOnLetterChangeListener = onLetterChangeListener;
    }

    public IndexBar(Context context) {
        this(context, null);
    }

    public IndexBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndexBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mRect = new Rect();
        mPaint.setColor(Color.WHITE);
        mPaint.setAntiAlias(true);
        //mPaint.setTypeface(Typeface.DEFAULT_BOLD);
        wordSize = convertDpToPixel(wordSize, getContext());
        preHeight = convertDpToPixel(preHeight, getContext());
        mPaint.setTextSize(preHeight);
        mPaint.setTextAlign(Paint.Align.CENTER);
    }

    public static int convertDpToPixel(float dp, Context context){
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        int px = (int)(dp * (metrics.densityDpi / 160f) + 0.5);
        return px;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if(letters!=null){
            heightCenter = getMeasuredHeight()/2 - preHeight*letters.size()/2;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(letters!=null) {
            /*for (int i = 0; i < letters.size(); i++) {
                String text = letters.get(i);
                float textWidth = mPaint.measureText(text);
                float x = mCellWidth * 1.0f - textWidth * 0.5f;
                FontManager.setPaintFont(mPaint, FontManager.getInstance().getDefaultType());
                mPaint.getTextBounds(text, 0, text.length(), mRect);
                float textHeight = mRect.height();
                float y = mCellHeight * 0.5f + textHeight * 0.5f + i * mCellHeight;
                mPaint.setColor(i == mIndex ? getResources().getColor(R.color.ganshi_ded8d7) : getResources().getColor(R.color.orange_ff755a));
                canvas.drawText(text, x, y, mPaint);
            }*/
            for (int i = 0; i < letters.size(); i++) {
                mPaint.setColor(i == mIndex ? getResources().getColor(R.color.ganshi_ded8d7) : ResourceUtil.getThemeColor());
                canvas.drawText(String.valueOf(letters.get(i)), getMeasuredWidth()/2, preHeight
                        + (i * preHeight) + heightCenter, mPaint);
            }
        }
        super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
       /* float y;
        int currentIndex;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                y = event.getY();
                currentIndex = (int) (y / mCellHeight);
                if (currentIndex>0 && currentIndex < letters.size() && mIndex != currentIndex) {
                    if (mOnLetterChangeListener != null) {
                        mOnLetterChangeListener.onLetterChange(letters.get(currentIndex));
                        mIndex = currentIndex;
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                y = event.getY();
                currentIndex = (int) (y / mCellHeight);
                if (currentIndex>0 && currentIndex < letters.size()&& mIndex != currentIndex) {
                    if (mOnLetterChangeListener != null) {
                        mOnLetterChangeListener.onLetterChange(letters.get(currentIndex));
                        mIndex = currentIndex;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                mIndex = -1;
                break;
            default:
                break;
        }
        invalidate();
        return true;*/
        int startY = (int) event.getY() - heightCenter;
        int index = startY / preHeight;
        if (index >= letters.size()) {
            index =letters.size() - 1;
        } else if (index < 0) {
            index = 0;
        }
        if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
            int position = mSectionIndexter.getStartPositionOfSection(String.valueOf(letters.get(index)));
            if (position == -1) {
                return true;
            }
            mListView.setSelection(position);
        }
        return true;
    }

    public void setListView(ListView listView) {
        mListView = listView;
        Adapter adapter = mListView.getAdapter();
        if (adapter == null || !(adapter instanceof Indexer)) throw new RuntimeException("ListView must set Adapter or Adapter must implements Indexer interface");
        mSectionIndexter = (Indexer) adapter;

    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if ((letters!=null)){
            mHeight = getMeasuredHeight();
            mCellWidth = getMeasuredWidth();
            mCellHeight = mHeight * 1.0f / letters.size();
        }

    }
}
